# Code for Table M5: Alternative outcome

rm(list = ls())

## ---------------------------------------
## Load Packages 
## ---------------------------------------

library('AER')
library('ivpack')
library('dplyr')
library('data.table')

## ---------------------------------------
## Load Data and Functions
## ---------------------------------------
load("../data0812.RData")

directory <- "../functions/"
functions <- list.files(directory)  
loadfunctions <- sapply(functions, FUN = function(x)source(paste0(directory, x)))

## ---------------------------------------
# Construct Instrument
## ---------------------------------------
data0812 <- constructIV(data0812)

## ---------------------------------------
#  Select Last Case Before Election
## ---------------------------------------
data0812 <- lastCase(data0812)


## ---------------------------------------
## Two Stage Least Squares Specifications
## ---------------------------------------
time.controls <- "as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow) + as.factor(court_shift) + as.factor(totOGS2)"
case.controls <-   "as.factor(any_drug_2) +  as.factor(any_violent_2) + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2) + as.factor(prior_offender_2)"
demo.controls_r <- "age_2012 + I(age_2012^2) + Female + as.factor(race)"

## ---------------------------------------
## Row 1: Main Result
## ---------------------------------------
outc.r2 <- "regis_after"
endo.1 <- "pti"
inst.1 <- "judgeiv"

form.1r2 <- formula(paste(outc.r2, "~", endo.1, "+" , time.controls, "|", inst.1, "+", time.controls))
form.2r2 <- formula(paste(outc.r2, "~", endo.1, "+" , time.controls, "+", demo.controls_r, "|", inst.1, "+", time.controls, "+", demo.controls_r))
form.3r2 <- formula(paste(outc.r2, "~", endo.1, "+" , time.controls, "+", demo.controls_r, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls_r, "+", case.controls))

m1a1 <- ivreg(form.1r2, data = data0812[noteli08==0], weights = (1 - regis_before))
m1a2 <- ivreg(form.2r2, data = data0812[noteli08==0], weights = (1 - regis_before))
m1a3 <- ivreg(form.3r2, data = data0812[noteli08==0], weights = (1 - regis_before))

m1a1_d <- summary(m1a1, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]
m1a2_d <- summary(m1a2, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]
m1a3_d <- summary(m1a3, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]

nc <- round(sum(1 - data0812$regis_before[data0812$noteli08==0]), 0)

row1 <- data.table(round(rbind(c(robust.se(m1a1)[c("pti"), c(1)], robust.se(m1a2)[c("pti"), c(1)], robust.se(m1a3)[c("pti"), c(1)]),
                               c(robust.se(m1a1)[c("pti"), c(2)], robust.se(m1a2)[c("pti"), c(2)], robust.se(m1a3)[c("pti"), c(2)]),
                               c(m1a1_d, m1a2_d, m1a3_d),
                               rep(nc, 3),
                               rep(NA, 3)
),3))


## ---------------------------------------
## Row 2: Racially Disparate Impact
## ---------------------------------------
endo.1 <- "pti * as.factor(race)"
inst.1 <- "judgeiv * as.factor(race)"

form.1r2 <- formula(paste(outc.r2, "~", endo.1, "+" , time.controls, "|", inst.1, "+", time.controls))
form.2r2 <- formula(paste(outc.r2, "~", endo.1, "+" , time.controls, "+", demo.controls_r, "|", inst.1, "+", time.controls, "+", demo.controls_r))
form.3r2 <- formula(paste(outc.r2, "~", endo.1, "+" , time.controls, "+", demo.controls_r, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls_r, "+", case.controls))

m1a1 <- ivreg(form.1r2, data = data0812[noteli08==0], weights = (1 - regis_before))
m1a2 <- ivreg(form.2r2, data = data0812[noteli08==0], weights = (1 - regis_before))
m1a3 <- ivreg(form.3r2, data = data0812[noteli08==0], weights = (1 - regis_before))


m1a1_d1 <- summary(m1a1, vcov = sandwich, diagnostic = TRUE)$diagnostics["Weak instruments (pti)",3]
m1a2_d1 <- summary(m1a2, vcov = sandwich, diagnostic = TRUE)$diagnostics["Weak instruments (pti)",3]
m1a3_d1 <- summary(m1a3, vcov = sandwich, diagnostic = TRUE)$diagnostics["Weak instruments (pti)",3]

m1a1_d2 <- summary(m1a1, vcov = sandwich, diagnostic = TRUE)$diagnostics["Weak instruments (pti:as.factor(race)White)",3]
m1a2_d2 <- summary(m1a2, vcov = sandwich, diagnostic = TRUE)$diagnostics["Weak instruments (pti:as.factor(race)White)",3]
m1a3_d2 <- summary(m1a3, vcov = sandwich, diagnostic = TRUE)$diagnostics["Weak instruments (pti:as.factor(race)White)",3]

nc <- round(sum(1 - data0812$regis_before[data0812$noteli08==0]), 0)

row2 <- data.table(round(rbind(# estimates
  c(robust.se(m1a1)[c("pti"), c(1)], robust.se(m1a2)[c("pti"), c(1)],  robust.se(m1a3)[c("pti"), c(1)]),
  # standard errors
  c(robust.se(m1a1)[c("pti"), c(2)], robust.se(m1a2)[c("pti"), c(2)], robust.se(m1a3)[c("pti"), c(2)]),
  # f statistics
  c(m1a1_d1, m1a2_d1, m1a3_d1),
  # estimates
  c(robust.se(m1a1)[c( "pti:as.factor(race)White"), c(1)], 
    robust.se(m1a2)[c("pti:as.factor(race)White"), c(1)], 
    robust.se(m1a3)[c("pti:as.factor(race)White"), c(1)]),
  # standard errors
  c(robust.se(m1a1)[c("pti:as.factor(race)White"), c(2)], 
    robust.se(m1a2)[c("pti:as.factor(race)White"), c(2)], 
    robust.se(m1a3)[c("pti:as.factor(race)White"), c(2)]),
  # f statistics
  c(m1a1_d2, m1a2_d2, m1a3_d2),
  # obs
  rep(nc, 3),
  rep(NA, 3)
),3))

## ---------------------------------------
## Print Table M5
## ---------------------------------------

cont <- rbind(row1, row2)
# add column labels
colnames(cont) <- c("(1)", "(2)", "(3)")
# add row labels
cont$names <-  c("Main result: PTI",
                 "Std. Error",
                 "F-test", 
                 "Obs.",
                 "",
                 "PTI (Black as baseline)",
                 "Std. Error",
                 "F-test", 
                 "PTI × White",
                 "Std. Error",
                 "F-test", 
                 "Obs.",
                 "")
cont <- cont[, c("names", "(1)", "(2)", "(3)")]
cat("\nPrinting Table M5: Alternative Outcome...\n")
print(cont)




